package com.xej.seat.service.impl;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xej.seat.mapper.CustomerMapper;
import com.xej.seat.mapper.SeatMapper;
import com.xej.seat.service.SeatService;
import com.xej.seat.vo.Customer;
import com.xej.seat.vo.ManualSeat;
import com.xej.seat.vo.Pager;
import com.xej.seat.vo.Seat;
/**
 * @author lisheng
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SeatServiceImpl implements SeatService {

    @Resource
    private SeatMapper seatMapper;
    
    @Resource
    private CustomerMapper customerMapper;
    @Override
    public int getSeatCount() {
        return seatMapper.getSeatCount();
    }

    @Override
    public int initSeat(Seat seat) {
    	List<Seat> list = new LinkedList<Seat>();
    	int row = seat.getSrow();
    	int col = seat.getScol();
    	int count = 1;
    	for (int i = 1; i <= row ; i++) {
    		for (int j = 1; j <= col; j++) {
				Seat st = new Seat();
				st.setSid(count);
				st.setSrow(i);
				st.setScol(j);
				st.setRuleId(seat.getRuleId());
				st.setHallId(seat.getHallId());
				st.setWeight(row + 1, col);
				list.add(st);
				count++;
			}
		}
    	return seatMapper.initSeat(list);
    }

    @Override
    public List<Seat> loadSeat(Pager pager, String cusName) {
    	int cusId = 0;
    	if(cusName != null && !"".equals(cusName)) {
    		// 根据userName查询userId，放进Map中
        	Customer cus = this.customerMapper.queryCustomerByName(cusName);
        	if(cus != null) {
        		cusId = cus.getCusId();
        	}
    	}
        return seatMapper.loadSeat(pager, cusId);
    }
    @Override
    public List<Seat> loadSeatList() {
    	return seatMapper.loadSeatList();
    }

	@Override
	public void addSeat(Seat seat) {
		this.seatMapper.addSeat(seat);
	}
	
	@Override
	public void editSeat(Map<String, Object> params) {
		this.seatMapper.editEdit(params);
		
	}
	
    @Override
    public void initPage(Pager pager) {
        int count = seatMapper.getSeatCount();
        pager.setTotalCount(count);
    }

	@Override
	public List<Seat> querySeatByParams(Map<String, Object> params) {
		return this.seatMapper.querySeatByParams(params);
	}

	@Override
	public List<Seat> querySeatByHallId(String hallId) {
		return this.seatMapper.querySeatByHallId(hallId);
	}

	@Override
	public int deleteAllSeat() {
		return this.seatMapper.deleteAllSeat();
	}
	
	@Override
	public List<ManualSeat> getManualSeatList(JSONArray manualSeatList) {
		List<ManualSeat> manualSeat = new ArrayList<ManualSeat>();
		if(manualSeatList != null && !manualSeatList.isEmpty()) {
			for(Object obj : manualSeatList) {
				ManualSeat ms = ((JSONObject) obj).toJavaObject(ManualSeat.class);
				manualSeat.add(ms);
			}
		}
		return manualSeat;
	}
	
}
